package com.sardine.base.esm.builder;

import com.sardine.base.esm.annotation.*;
import com.sardine.base.utils.JsonUtils;
import com.sardine.base.esm.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * 上海京东到家元信信息技术有限公司<br>
 * Created with IntelliJ IDEA.
 * Date:2017/5/22
 * Time:16:47
 * ESM 的 注解构建（临时）
 * @author daifeng<br>
 *         daifeng@imdada.cn
 * @since JDK 1.7 <br>
 * Copyright (c) 2017 imdada System Incorporated All Rights Reserved.
 */
public class EsmAnnotationBuilder {
    private static final Logger logger = LoggerFactory.getLogger(EsmAnnotationBuilder.class);

    /**
     * 构建数据。验证后续添加
     * @param esm
     * @return
     */
    public static EsmEntity builder(Esm esm){
        EsmEntity entity=new EsmEntity();
        Class<?> aClass = esm.getClass();
        if(aClass.isAnnotationPresent(EsmTypeName.class)){
            EsmTypeName annotation = aClass.getAnnotation(EsmTypeName.class);
            String typeName = annotation.value();
            entity.setTypeName(typeName);
            entity.setReadAlias(annotation.readIndexAlias());
            entity.setWriteAlias(annotation.writeIndexAlias());
            String pattern = annotation.sourceDateFormat();
            if(StringUtils.isBlank(pattern)){
                pattern="yyyy-MM-dd HH:mm:ss";
            }
            entity.setSource(JsonUtils.toJsonWithDateFormat(esm, pattern));
        }

        // 读取属性的注释
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            //判断是否静态属性
            if (Modifier.isStatic(field.getModifiers())) {
                continue;
            }
            field.setAccessible(true);


            //-----------------------------------------------------------------
            EsmDocId docId = field.getAnnotation(EsmDocId.class);// 取得指定注释
            if(docId !=null)
            {
                Object value = null;
                try {
                    value = field.get(esm);
                } catch (IllegalAccessException e) {
                }
                entity.setId(value.toString());
            }

            //-----------------------------------------------------------------
            EsmRoutingName routingName = field.getAnnotation(EsmRoutingName.class);// 取得指定注释
            if(routingName !=null)
            {
                Object value = null;
                try {
                    value = field.get(esm);
                } catch (IllegalAccessException e) {
                }
                entity.setRouting(value.toString());
            }
        }
        return entity;
    }

    public static <T> T parseObject(String text, Class<T> clazz) {
        return JsonUtils.fromJson(text, clazz);
    }


    /**
     * 查询typeName
     * @param clazz
     * @return
     */
    public static String builderTypeName(Class<?> clazz) {
        if(clazz.isAnnotationPresent(EsmTypeName.class)){
            EsmTypeName annotation = clazz.getAnnotation(EsmTypeName.class);
            String typeName = annotation.value();
           return typeName;
        }
        return null;
    }
}
